/*
 * visorErradas.java
 *
 * Created on 15 de septiembre de 2008, 05:51 PM
 */

package administrador;
import java.util.Calendar;
public class visorErradas extends javax.swing.JFrame {
    
    /** Creates new form visorErradas */
    leeArchCarpeta archivos=new leeArchCarpeta();
    imprimeErradas imp=new imprimeErradas();
    javax.swing.JTable tabla=null;
    javax.swing.JScrollPane js=null;
    calendario.JCalendar jCalendar = null;
    public visorErradas() {
        initComponents();
        setVisible(true);
        jCalendar = new calendario.JCalendar(this, "Calendario", true);
        ponerFechas();
    }
     java.sql.Date fecha1=null;
    java.sql.Date fecha2=null;
    public void sacarFecha(int opcion) {
        Calendar cDate = jCalendar.getCalendar();
        if (cDate == null) {
            cDate = Calendar.getInstance();
        }
        jCalendar.setCalendar(cDate);
        jCalendar.setVisible(true);
        if (jCalendar.isOkPressed()) {
            if (opcion == 1) {
                fecha1 = new java.sql.Date(jCalendar.getDate().getTime());
                System.out.println(fecha1);
                txt_fecha1.setText(fecha1.toString());
            } else if (opcion == 2) {
                fecha2 = new java.sql.Date(jCalendar.getDate().getTime());
                txt_fecha2.setText(fecha2.toString());
            }
            jCalendar.setVisible(false);
        }
    }
    public void ponerFechas(){
    String fechaActual=new java.sql.Date(System.currentTimeMillis()).toString();
    txt_fecha1.setText(fechaActual);
    txt_fecha2.setText(fechaActual);
    }
    
    public String fechaFormateada(String fecha){
        fecha=fecha.replaceAll("-", "");
        return fecha;
    }
    public void muestroTabla(){
        String fechaInicio=fechaFormateada(txt_fecha1.getText());
        String fechaFin=fechaFormateada(txt_fecha2.getText());
        java.util.Vector vectorArchivos=archivos.filtroArchivo(fechaInicio,fechaFin,comboDoc.getSelectedItem().toString());
        int filas=vectorArchivos.size();
        String[] titulosCol={"Codigo","Fecha","Hora","Codigo"};
        CrearTabla(filas,titulosCol);
        llenaTabla(vectorArchivos);
    }
    public void CrearTabla(int filas,String[] titulos){
        
        int columnas=titulos.length;
        String[][] tabla0=new String[filas][columnas];
       
        tabla= new javax.swing.JTable(tabla0,titulos);
        
        //Aqui le especifico para que la tabla acepte valores doubles e int:
        //--------------------------------------------------------------------
        final boolean[] canEdit = new boolean [columnas];
        for(int p=0;p<columnas;p++){
        canEdit[p]=false;
        }
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            tabla0,titulos  
        ) {
           // boolean[] canEdit = new boolean [] { false, false, false, false};
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        //---------------------------------------------------------------------
                   
        //Le doy el evento del LCick del Mouse:
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        
        //remuevo el JScroll para que no se monte cuando cree el nuevo:
        //*************************************************************
        try{
            panel_tabla.remove(js);
        }catch(java.lang.NullPointerException e){}
        //*************************************************************
        //Pongo en la tabla:
            js = new javax.swing.JScrollPane(tabla);
            js.setBounds(0,0,700,500);
            js.setVisible(true);
            panel_tabla.add(js);
            js.updateUI();
    }
    private void tablaMouseClicked(java.awt.event.MouseEvent evt){
            String documento=tabla.getValueAt(tabla.getSelectedRow(),0).toString();
            String fecha=tabla.getValueAt(tabla.getSelectedRow(),1).toString().replaceAll("-","");
            String hora=tabla.getValueAt(tabla.getSelectedRow(),2).toString().replaceAll(":","");
            String codigo=tabla.getValueAt(tabla.getSelectedRow(),3).toString();
            visorFactura v=new visorFactura(fecha+"_"+hora+"_"+documento+"_Errada_"+codigo);
    }
    public void llenaTabla(java.util.Vector vectorArchivos){
        imp.facturaTXT.borrarArchivo(imp.facturaTXT.sacoFile());
        java.util.Vector vectorint=new java.util.Vector();
        imp.escriboFact("Documento"+"\t"+"Fecha"+"\t"+"Hora"+"\t"+"Codigo"+"\n");
        for(int i=0;i<vectorArchivos.size();i++){
            vectorint=(java.util.Vector)vectorArchivos.elementAt(i);
            tabla.setValueAt(vectorint.elementAt(0), i, 0);
            tabla.setValueAt(vectorint.elementAt(1), i, 1);
            tabla.setValueAt(vectorint.elementAt(2), i, 2);
            tabla.setValueAt(vectorint.elementAt(3), i, 3);
            imp.escriboFact(vectorint.elementAt(0)+"\t"+vectorint.elementAt(1)+"\t"+vectorint.elementAt(2)+"\t"+vectorint.elementAt(3)+"\n");
        }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel_tabla = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_fecha1 = new javax.swing.JTextField();
        txt_fecha2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        comboDoc = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        btn_imprimir = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        panel_tabla.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panel_tabla.setName("panel_tabla"); // NOI18N

        javax.swing.GroupLayout panel_tablaLayout = new javax.swing.GroupLayout(panel_tabla);
        panel_tabla.setLayout(panel_tablaLayout);
        panel_tablaLayout.setHorizontalGroup(
            panel_tablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 641, Short.MAX_VALUE)
        );
        panel_tablaLayout.setVerticalGroup(
            panel_tablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 253, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(visorErradas.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txt_fecha1.setEditable(false);
        txt_fecha1.setName("txt_fecha1"); // NOI18N

        txt_fecha2.setEditable(false);
        txt_fecha2.setName("txt_fecha2"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        comboDoc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Factura", "Nota de Venta", "Nota de Debito", "Nota de Credito" }));
        comboDoc.setName("comboDoc"); // NOI18N
        comboDoc.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboDocItemStateChanged(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btn_imprimir.setText(resourceMap.getString("btn_imprimir.text")); // NOI18N
        btn_imprimir.setName("btn_imprimir"); // NOI18N
        btn_imprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_imprimirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_fecha2, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                    .addComponent(txt_fecha1, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_imprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(83, 83, 83))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_fecha1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txt_fecha2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(comboDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(27, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(46, Short.MAX_VALUE)
                .addComponent(btn_imprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setForeground(resourceMap.getColor("jLabel4.foreground")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panel_tabla, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel4)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(panel_tabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-704)/2, (screenSize.height-487)/2, 704, 487);
    }// </editor-fold>//GEN-END:initComponents
boolean cargado=false;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
 buscar();
}//GEN-LAST:event_jButton1ActionPerformed
public void buscar(){
  
    if(txt_fecha1.getText().equals("")){
       javax.swing.JOptionPane.showMessageDialog(this,"Ingrese la Fecha de Inicio","Peligro",javax.swing.JOptionPane.WARNING_MESSAGE); 
       cargado=false;
   }else if(txt_fecha2.getText().equals("")){
       javax.swing.JOptionPane.showMessageDialog(this,"Ingrese la Fecha de Fin","Peligro",javax.swing.JOptionPane.WARNING_MESSAGE); 
       cargado=false;
   }else{
       muestroTabla();
       cargado=true;
   }

}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    sacarFecha(1);
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    sacarFecha(2);
}//GEN-LAST:event_jButton3ActionPerformed

private void btn_imprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_imprimirActionPerformed
    if(cargado==true) 
        imp.realizaImprecion();
    else
        javax.swing.JOptionPane.showMessageDialog(this,"No ha cargado los datos","Peligro",javax.swing.JOptionPane.WARNING_MESSAGE); 
}//GEN-LAST:event_btn_imprimirActionPerformed

private void comboDocItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboDocItemStateChanged
// TODO add your handling code here:
    buscar();
}//GEN-LAST:event_comboDocItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_imprimir;
    private javax.swing.JComboBox comboDoc;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panel_tabla;
    private javax.swing.JTextField txt_fecha1;
    private javax.swing.JTextField txt_fecha2;
    // End of variables declaration//GEN-END:variables

}
